package com.yjpark.sample.fileio;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class FileIOSampleActivity extends Activity {
	EditText mEdit;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		mEdit = (EditText) findViewById(R.id.edit);
		findViewById(R.id.save).setOnClickListener(mClickListener);
		findViewById(R.id.load).setOnClickListener(mClickListener);
	}

	View.OnClickListener mClickListener = new View.OnClickListener() {
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.save:
				try {
					FileOutputStream fos = openFileOutput("test.txt",
							Context.MODE_WORLD_READABLE);
					String str = "Android File IO Test";
					fos.write(str.getBytes());
					fos.close();
					mEdit.setText("write success");
				} catch (Exception e) {
				}
				break;
			case R.id.load:
				try {
					FileInputStream fis = openFileInput("test.txt");
					byte[] data = new byte[fis.available()];
					while (fis.read(data) != -1) {
						;
					}
					fis.close();
					mEdit.setText(new String(data));
				} catch (Exception e) {
				}
				break;
			}
		}
	};
}